<?php
// 初始化一副扑克牌
function initPoker() {
    $suits = ['♠', '♥', '♣', '♦'];
    $ranks = ['3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '2'];
    $poker = [];

    // 生成普通牌
    foreach ($suits as $suit) {
        foreach ($ranks as $rank) {
            $poker[] = $suit . $rank;
        }
    }

    // 添加大小王
    $poker[] = '大王';
    $poker[] = '小王';

    return $poker;
}

// 洗牌函数
function shufflePoker(&$poker) {
    shuffle($poker);
}

// 发牌函数
function dealPoker($poker) {
    $players = [
        'player1' => [],
        'player2' => [],
        'player3' => []
    ];
    $bottomCards = [];

    for ($i = 0; $i < count($poker); $i++) {
        if ($i >= count($poker) - 3) {
            $bottomCards[] = $poker[$i];
        } else {
            $playerIndex = $i % 3;
            switch ($playerIndex) {
                case 0:
                    $players['player1'][] = $poker[$i];
                    break;
                case 1:
                    $players['player2'][] = $poker[$i];
                    break;
                case 2:
                    $players['player3'][] = $poker[$i];
                    break;
            }
        }
    }

    return [
        'players' => $players,
        'bottomCards' => $bottomCards
    ];
}

// 初始化扑克牌
$poker = initPoker();
// 洗牌
shufflePoker($poker);
// 发牌
$result = dealPoker($poker);
$players = $result['players'];
$bottomCards = $result['bottomCards'];
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>斗地主游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }

        .player {
            margin-bottom: 20px;
        }

        .player h2 {
            margin-bottom: 10px;
        }

        .card {
            display: inline-block;
            border: 1px solid #ccc;
            padding: 5px 10px;
            margin: 2px;
            background-color: #fff;
        }
    </style>
</head>

<body>
    <?php foreach ($players as $playerName => $hand) : ?>
        <div class="player">
            <h2><?php echo $playerName; ?></h2>
            <?php foreach ($hand as $card) : ?>
                <span class="card"><?php echo $card; ?></span>
            <?php endforeach; ?>
        </div>
    <?php endforeach; ?>

    <div class="player">
        <h2>底牌</h2>
        <?php foreach ($bottomCards as $card) : ?>
            <span class="card"><?php echo $card; ?></span>
        <?php endforeach; ?>
    </div>
</body>

</html>